package cn.yinyu.queue.module.ecg.service.appointment;

import cn.yinyu.queue.framework.common.util.date.DateUtils;
import cn.yinyu.queue.framework.common.util.date.LocalDateTimeUtils;
import cn.yinyu.queue.module.ecg.controller.admin.queue.vo.QueueSaveReqVO;
import cn.yinyu.queue.module.ecg.dal.dataobject.checktype.CheckTypeDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.patient.PatDetails;
import cn.yinyu.queue.module.ecg.dal.dataobject.queue.QueueDO;
import cn.yinyu.queue.module.ecg.feign.RemoteDataService;
import cn.yinyu.queue.module.ecg.feign.RestApiReqBodyVo;
import cn.yinyu.queue.module.ecg.feign.RestApiResult;
import cn.yinyu.queue.module.ecg.feign.dto.AppointmentExternal;
import cn.yinyu.queue.module.ecg.service.config.EcgConfigService;
import cn.yinyu.queue.module.ecg.service.queue.QueueService;
import cn.yinyu.queue.module.ecg.service.queue.QueueServiceTxFunctions;
import cn.yinyu.queue.module.ecg.service.queuesequence.QueueSequenceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import cn.yinyu.queue.module.ecg.controller.admin.appointment.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.appointment.AppointmentDO;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;

import cn.yinyu.queue.module.ecg.dal.mysql.appointment.AppointmentMapper;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.yinyu.queue.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.error;
import static cn.yinyu.queue.module.ecg.enums.ErrorCodeConstants.*;

/**
 * 预约 Service 实现类
 *
 * @author 马剑波
 */
@Service
@Validated
@Slf4j
public class AppointmentServiceImpl implements AppointmentService {

    @Resource
    private EcgConfigService ecgConfigService;

    @Resource
    private RemoteDataService remoteDataService;

    @Resource
    private QueueSequenceService queueSequenceService;

    @Resource
    private QueueService queueService;

    @Resource
    private AppointmentMapper appointmentMapper;

    @Override
    public Long createAppointment(AppointmentSaveReqVO createReqVO) {
        // 插入
        AppointmentDO appointment = BeanUtils.toBean(createReqVO, AppointmentDO.class);

        appointment.setBookTime( LocalDateTime.now() );

        try {
            appointmentMapper.insert(appointment);
        } catch (DuplicateKeyException e) {
            throw exception(APPOINTMENT_EXIST_TODAY);
        }

        // 返回
        return appointment.getId();
    }

    @Override
    public void updateAppointment(AppointmentSaveReqVO updateReqVO) {
        // 校验存在
        validateAppointmentExists(updateReqVO.getId());
        // 更新
        AppointmentDO updateObj = BeanUtils.toBean(updateReqVO, AppointmentDO.class);
        appointmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteAppointment(Long id) {
        // 校验存在
        validateAppointmentExists(id);
        // 删除
        appointmentMapper.deleteById(id);
    }

    private void validateAppointmentExists(Long id) {
        if (appointmentMapper.selectById(id) == null) {
            throw exception(APPOINTMENT_NOT_EXISTS);
        }
    }

    @Override
    public AppointmentDO getAppointment(Long id) {
        return appointmentMapper.selectById(id);
    }

    @Override
    public AppointmentDO getCurAppointmentByPatIdAndCheckType(String patId, Integer checkType) {
        AppointmentDO appointmentDO = appointmentMapper.getByPatAndCheckTypeAndBookDate( patId, checkType, LocalDate.now() );
        return appointmentDO;
    }

    /**
     * @param patCode 编号 : !!! 不一定就是本系统的 患者编号(patID), 根源在于 检查单扫描出来的编号 不一定是 本系统的 患者编号(patID)
     * @return
     */
    @Override
    public List<AppointmentDO> queryAndCreateAppointmentByPatId(String patCode) {
        // 从医院平台查询
        List<AppointmentDO> appointmentExtermalList = getAppointmentExtermal( patCode );

        for ( int appointmentIndex = 0; appointmentIndex < appointmentExtermalList.size(); appointmentIndex++ ) {
            AppointmentDO appointmentExtermal = appointmentExtermalList.get(appointmentIndex);
            if (null == appointmentExtermal )
                continue;

            // 从DB查询预约，是否已经存在
            AppointmentDO appointmentDO = appointmentMapper.getByEpisodeIdAndApplyNo(appointmentExtermal.getEpisodeId(), appointmentExtermal.getApplyNo());
            if (null == appointmentDO) {
                appointmentMapper.insert(appointmentExtermal);
            } else {
                appointmentExtermal.setId( appointmentDO.getId() ); // 确保 返回值包含 appointment id

                // 处理第一次确认时还没有预约时间，再来确认时已有预约时间的情况
                if (appointmentDO.getBookDate() != appointmentExtermal.getBookDate()) {
                    appointmentMapper.updateById( appointmentExtermal );
                }
            }
        }

        // 查询[queue表]补充 book_seq_num 到返回值中
        if (!appointmentExtermalList.isEmpty()) {
            List<Long> appointIdlist = appointmentExtermalList.stream().map(appointmentDO -> appointmentDO.getId()).toList();
            List<QueueDO> simpleQueueDOList = queueService.selectBookSeqNumByAppointIdList(appointIdlist);
            if (!simpleQueueDOList.isEmpty()) {
                Map<Long, Integer> mapAppointIdVsBookSeqNo = simpleQueueDOList.stream().collect(Collectors.toMap(QueueDO::getAppointId, QueueDO::getBookSeqNum));
                appointmentExtermalList.forEach(appointmentExterma -> appointmentExterma.setBookSeqNum(mapAppointIdVsBookSeqNo.get(appointmentExterma.getId())));
            }
        }

        // 不从DB查询
        //List<AppointmentDO> appointmentDOList = appointmentMapper.getCurrentPatId( patCode );
        //return 0 == appointmentDOList.size() ? null : appointmentDOList.get(0);

        return appointmentExtermalList;
    }

    @Override
    public PageResult<AppointmentDO> getAppointmentPage(AppointmentPageReqVO pageReqVO) {
        return appointmentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppointmentDO> getAppointmentExtermal(String patId) {
        List<AppointmentDO> curAppointDOList = new ArrayList<AppointmentDO>();

        RestApiReqBodyVo reqBodyVo = new RestApiReqBodyVo();
        if (18 == patId.length()) {
            reqBodyVo.setSfzh( patId );
        } else {
            reqBodyVo.setMzzyh(patId);
        }

        // QueryRisReportList   queryEcgRequest
        RestApiResult<AppointmentExternal> result = remoteDataService.httpApi("queryEcgRequest", "ECG", "ECG", reqBodyVo);
        if (0 == result.getRow().size()) {
            return curAppointDOList;
        }

        for (int appointIndex=0; appointIndex < result.getRow().size(); appointIndex++ ) {
            AppointmentExternal appointmentExternal = result.getRow().get( appointIndex );

            AppointmentDO appointmentDO = BeanUtils.toBean(appointmentExternal, AppointmentDO.class);
            appointmentDO.setApplyNo( appointmentExternal.getReqIdeApplyno() );
            appointmentDO.setEpisodeId( appointmentExternal.getEpisodeID() );
            appointmentDO.setPatSrc( getCorrespondingPatientSource(appointmentExternal.getAdmTypeCode()) );
            appointmentDO.setPatId(appointmentExternal.getPatientID()); // 内容为 身份证号 或 门诊住院号
            appointmentDO.setPatName(appointmentExternal.getPatName());
            appointmentDO.setPatGender(Byte.valueOf(appointmentExternal.getPatgender()));
            appointmentDO.setPatBirthday(DateUtils.ofUTC(appointmentExternal.getEncPatBirthDate()).toLocalDate());
            appointmentDO.setPatIdentityId(appointmentExternal.getIdentityID());
            appointmentDO.setPatAddr(appointmentExternal.getAddress());
            appointmentDO.setPatDeptCode(appointmentExternal.getPatLocDeptCode());
            appointmentDO.setPatDeptDesc(appointmentExternal.getPatLocDeptDesc());
            appointmentDO.setPatWardCode(appointmentExternal.getPatLocWardCode());
            appointmentDO.setPatWardDesc(appointmentExternal.getPatLocWardDesc());
            appointmentDO.setPatMobile(appointmentExternal.getPhone());
            appointmentDO.setPatBedNo(appointmentExternal.getEnBedno());
            appointmentDO.setBookCheckType(getCorrespondingCheckType(appointmentExternal.getPlanDefItemList().getPlanDefItem().getPlanDefItemcode()));
            appointmentDO.setBookTime(DateUtils.ofUTC(appointmentExternal.getReqAuthoredOn()));  // 开单时间
            appointmentDO.setBookSrc( 0 );

            // 光开单的情况，没有预约时间
            if (null != appointmentExternal.getReqExtBooktime() ) {
                appointmentDO.setBookDate(DateUtils.ofUTC(appointmentExternal.getReqExtBooktime()).toLocalDate());
                LocalDateTime bookStartTime = DateUtils.ofUTC(appointmentExternal.getReqExtBooktime());
                LocalDateTime bookEndTime = bookStartTime.plusMinutes( ecgConfigService.getTimeslotLength());
                appointmentDO.setBookTimeslot((bookStartTime.getHour() * 100 + bookStartTime.getMinute()) * 10000 + bookEndTime.getHour() * 100 + bookEndTime.getMinute());
            }

            appointmentDO.setPaid( 0 );

            curAppointDOList.add( appointmentDO );
        }

        return curAppointDOList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer appoitmentConfirm(AppointmentConfirmReqVO confirmReqVO) {
        AppointmentDO appointment = getAppointment(confirmReqVO.getId());
        if (null == appointment)
            throw exception(PATIENT_NOT_EXISTS);

        // 手动预约，直接设置预约日期为当天
        if (confirmReqVO.getIsVip() == 1) {
            appointment.setBookDate( LocalDate.now() );
        }

        if ( !DateUtils.isToday(appointment.getBookDate()) )
            throw exception( APPOINTMENT_NOT_TODAY);


        Integer newSeqNo = null;
        if (confirmReqVO.getIsVip() == 0) {
            newSeqNo = queueSequenceService.distributeSeqNo(confirmReqVO.getBookCheckType(), confirmReqVO.getBookTimeslot());
        } else {
            newSeqNo = queueSequenceService.distributeVipSeqNo(confirmReqVO.getBookCheckType(), confirmReqVO.getBookTimeslot());
        }

        try {
            QueueSaveReqVO queueSaveReqVO = new QueueSaveReqVO();
            queueSaveReqVO.setApplyNo( appointment.getApplyNo() );
            queueSaveReqVO.setEpisodeId( appointment.getEpisodeId() );
            queueSaveReqVO.setAppointId(appointment.getId());
            queueSaveReqVO.setPatId(appointment.getPatId());
            queueSaveReqVO.setPatName(appointment.getPatName());
            queueSaveReqVO.setPatGender(appointment.getPatGender());
            queueSaveReqVO.setBookDate(appointment.getBookDate());
            queueSaveReqVO.setBookTimeslot( confirmReqVO.getBookTimeslot() ); /*appointment.getBookTimeslot()*/
            queueSaveReqVO.setBookCheckType(appointment.getBookCheckType());
            queueSaveReqVO.setBookSeqNum( newSeqNo );
            queueSaveReqVO.setIsVip( confirmReqVO.getIsVip() );
            queueSaveReqVO.setPassed((byte) 0);
            queueSaveReqVO.setExpired((byte) 0);
            queueSaveReqVO.setPatDetails( getPatDetails(appointment) );
            queueService.queue(queueSaveReqVO);
        } catch (DuplicateKeyException duplicateKeyException) {
            throw exception(APPOINTMENT_HAVE_QUEUED);
        }

        return newSeqNo;
    }

    private Integer getCorrespondingCheckType(String strPlanDefItemcode) {
        if (strPlanDefItemcode.equals("691133607"))
            return 100;
        else if (strPlanDefItemcode.equals("201605"))
            return 200;
        else if (strPlanDefItemcode.equals("200327"))
            return 300;
        else if (strPlanDefItemcode.equals("201652"))
            return 400;
        else if (strPlanDefItemcode.equals("502490914"))
            return 500;
        else if (strPlanDefItemcode.equals("419562119"))
            return 600;
        else if (strPlanDefItemcode.equals("201604"))
            return 700;
        else if (strPlanDefItemcode.equals("1202042"))
            return 800;
        else if (strPlanDefItemcode.equals("1202058"))
            return 900;
        else if (strPlanDefItemcode.equals("1202065"))
            return 1000;
        else if (strPlanDefItemcode.equals("559542128"))
            return 1100;
        else if (strPlanDefItemcode.equals("590244511"))
            return 1200;
        else if (strPlanDefItemcode.equals("666454217"))
            return 1300;
        else if (strPlanDefItemcode.equals("720791490"))
            return 1400;
        else if (strPlanDefItemcode.equals("720792077"))
            return 1500;

        return 100;
    }

    private Integer getCorrespondingPatientSource(String admTypeCode) {
        if (admTypeCode.equals("AMB"))
            return 1;
        else if (admTypeCode.equals("EMER"))
            return 2;
        else if (admTypeCode.equals("IMP"))
            return 3;
        else if (admTypeCode.equals("PHY"))
            return 4;

        return 0;
    }

    private PatDetails getPatDetails(AppointmentDO appointment) {
        PatDetails patDetails = new PatDetails();
        patDetails.setId( appointment.getPatId() );
        patDetails.setName( appointment.getPatName() );
        patDetails.setMobile( appointment.getPatMobile() );
        patDetails.setDeptCode( appointment.getPatDeptCode() );
        patDetails.setDeptDesc( appointment.getPatDeptDesc() );
        patDetails.setWardCode( appointment.getPatWardCode() );
        patDetails.setWardDesc( appointment.getPatWardDesc() );
        patDetails.setBedNo( appointment.getPatBedNo() );
        patDetails.setSource( appointment.getPatSrc() );
        return patDetails;
    }
}

